﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Windows.Forms;
using System.IO;

namespace DataAccessor
{
    /// <summary>
    /// 缓存Holistic的所有信息
    /// </summary>
    public static class HolisticCache
    {
        /// <summary>
        /// 总体配置文件的路径
        /// </summary>
        private static string _holisticFilePath = null;
        /// <summary>
        /// 获取总体配置文件的路径
        /// </summary>
        internal static string HolisticFilePath
        {
            get
            {
                if (string.IsNullOrEmpty(_holisticFilePath))
                {
                    var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data\T-SQL\Holistic.xml");

                    if (File.Exists(path))
                    {
                        _holisticFilePath = path;
                    }
                    else
                    {
                        throw new FileNotFoundException(string.Format("{0}文件未找到.", _holisticFilePath));
                    }
                }
                return _holisticFilePath;
            }
        }

        /// <summary>
        /// 开辟缓存区域
        /// </summary>
        private static Holistic _holistic;
        /// <summary>
        /// 获取所有模块的集合
        /// </summary>
        public static Holistic Holistic
        {
            get { return _holistic; }
        }

        static HolisticParser _parser = null;
        /// <summary>
        /// 静态构造函数，在调用类时初始化HolisticCache对象
        /// </summary>
        static HolisticCache()
        {
            _parser = new HolisticParser(HolisticFilePath);
            _holistic = _parser.Holistic;
        }

        /// <summary>
        /// 修改连接字符串
        /// </summary>
        /// <param name="databaseName">数据库配置节的名称</param>
        /// <param name="connectionString">新连接字符串</param>
        /// <returns>修改成功-true；修改失败-false.</returns>
        public static bool ChangeDatabaseConnectionString(string databaseName, string connectionString)
        {
            return _parser.ChangeDatabaseConnectionString(databaseName, connectionString);
        }
    }
}
